﻿@{
    ViewBag.Title = "CouponPromotion";
}
@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>
    <link href="~/Scripts/miniui/themes/default/medium-mode.css" rel="stylesheet" type="text/css" />      
}
<div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
    <table width = "1000px;">
        <tr>
            <td width = "1000px;">
                <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="NewBill" plain="true">新增</a>
                <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="save" plain="true">保存</a>
                <span class="separator"></span>
                <a class="mini-button" id="modify" name="modify" iconCls="icon-edit" onclick="Edit" plain="true">改单</a>
                <a class="mini-button" id="check" name="check" iconCls="icon-ok" onclick="check" plain="true">审核</a>
                <span class="separator"></span>
                <a class="mini-button" id="close" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
            </td>
        </tr>
    </table>
</div>

<form id="form1" method="post" style="height: 100%">
    <div id = "DivHeader">
                <table style="margin-top:0px;">
                    <tr>
                        <td width="80px" height ="25px" align="right">
                            方案编码：</td>
                        <td width="180px" height ="25px">
                            <input name="FNumber" id="FNumber" class="mini-textbox" style="width:180px;"  value="" TabIndex="1" maxLength="20" readonly="true"/>
                        </td>
                        <td width="80px" height ="25px" align="right">
                            促销人群：</td>
                        <td>
                            <input id="combo1" class="mini-combobox" style="width:80px;" textField="text" valueField="id" readonly="true"
                                data="CustType"  value="2"   allowInput="true" showNullItem="false"/>  
                        </td>
                         <td width="80px" height ="25px" align="right">
                            开始日期：</td>
                        <td width="120px" height ="25px">
                            <input class="mini-datepicker" name="FStartDate" format="yyyy-MM-dd" style="width:120px;"  value = ""/>
                        </td>
                        <td width="80px" height ="25px" align="right">
                            开始时间：</td>
                        <td width="100px" height ="25px">
                            <input id="FStartTime" name="FStartTime" class="mini-timespinner"  format="HH:mm"  style="width:100px;" />
                        </td>
                   </tr>
                    <tr>
                        <td width="80px" height ="25px" align="right">
                            方案名称：</td>
                        <td width="180px" height ="25px">
                            <input id="FName" name="FName" class="mini-textbox " style="width:180px;"  TabIndex="2"  maxLength="50" value="礼券促销"/>
                        </td>
                        <td width="80px" height ="25px" align="right">
                            商品范围：</td>
                        <td width="80px" height ="25px">
                            <input id="combo2" class="mini-combobox" style="width:80px;" textField="text" valueField="id"
                                data="Gtype" value="3"  allowInput="true" onvaluechanged="onComboxChanged()"/> 
                        <td width="80px" height ="25px" align="right">
                            结束日期：</td>
                        <td width="120px" height ="25px">
                            <input class="mini-datepicker" name="FEndDate" format="yyyy-MM-dd" style="width:120px;"  value = ""/>
                        </td>
                        <td width="80px" height ="25px" align="right">
                            结束时间：</td>
                        <td width="100px" height ="25px">
                            <input id="FEndTime" name="FEndTime" class="mini-timespinner"  format="HH:mm"  style="width:100px;" value="23:59"/>
                        </td>
                    </tr>
                    <tr>
                        <td width="80px" height ="35px" align="right">
                            星期选择：</td>
                        <td width="200px" height ="35px" colspan="5">
                            <div id="week1" name="week1"  class="mini-checkbox" readOnly="false" text="星期一" style="margin-right:7px;"></div>
                            <div id="week2" name="week2"  class="mini-checkbox" readOnly="false" text="星期二" style="margin-right:7px;"></div>
                            <div id="week3" name="week3"  class="mini-checkbox" readOnly="false" text="星期三" style="margin-right:7px;"></div>
                            <div id="week4" name="week4"  class="mini-checkbox" readOnly="false" text="星期四" style="margin-right:7px;"></div>
                            <div id="week5" name="week5"  class="mini-checkbox" readOnly="false" text="星期五" style="margin-right:7px;"></div>
                            <div id="week6" name="week6"  class="mini-checkbox" readOnly="false" text="星期六" style="margin-right:7px;"></div>
                            <div id="week0" name="week0"  class="mini-checkbox" readOnly="false" text="星期日" ></div>
                       </td>
                        <td colspan="2">
                            <div id="ckonline" name="ckonline" checked="true"  class="mini-checkbox" readOnly="false" text="线上"  style="margin-right:20px;"></div>
                            <div id="ckline" name="ckline" checked="true" class="mini-checkbox" readOnly="false" text="线下" onvaluechanged="GetMemberClass"></div>
                        </td>
                    </tr>
                    <tr>
                        <td width="80px" height ="25px" align="right">
                            礼券选择：</td>
                        <td width="180px" height ="25px">
                            @Html.Partial("~/Views/Modular/LookupCoupon.cshtml")
                        </td>
                        <td width="80px" height ="25px" align="right">
                            赠送数量：</td>
                        <td width="80px" height ="25px">
                            <input name="FSendQty" class="mini-spinner" style="width: 80px;" minvalue="0" maxvalue="100" 
                                showbutton="false" changeonmousewheel="false" increment ="0" value="1"/>
                        </td>
                        <td width="80px" height ="25px" align="right">
                            </td>
                        <td width="120px" height ="25px">
                        </td>
                        <td width="80px" height ="25px" align="right">
                            </td>
                        <td width="100px" height ="25px">
                        </td>
                    </tr>
                </table>
    </div>

    <div class="mini-fit" >
        <div id="tabs1" class="mini-tabs" activeIndex="0"  style="width:100%; height:100%; top: 0px; left: 0px;overflow:hidden;" plain="false"  buttons="#tabsButtons">
            <div id="SelectGood" name="SelectGood" title="商品设置" style="height:550px;overflow:hidden;margin-left:20px; margin-top:5px;">
                <div style="width:100%;">
                    <div class="mini-toolbar" style="border-bottom:0;padding:0px;">
                        <table style="width:100%;">
                            <tr>
                                <td style="width:450px;">
                                    <a class="mini-button" name="AddGood" iconCls="icon-add" onclick="addnew()">新增</a>       
                                    <a class="mini-button" name="DelGood" iconCls="icon-remove" onclick="remove()">删除</a>       
                                </td>
                                <td>
                                </td>
                            </tr>
                        </table>           
                    </div>
                </div>
                <div id="datagrid1" class="mini-datagrid" style="width:100%;height:88%;" 
                    showPager = "false" showPageSize="false" showPageIndex="false" skipReadOnlyCell="true"
                    allowCellSelect="true" allowSortColumn = "false" allowCellEdit="true" navEditMode="true"
                    editNextOnEnterKey="true"  editNextRowCell="false"
                    fitColumns="false">
                    <div property="columns">            
                        <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                        <div field="FNumber" name="mFNumber" displayField = "FNumber" width="120" headerAlign="center">商品编码</div>  
                        <div field="FName" name="mFName" width="160" headerAlign="center">商品名称</div>  
                        <div field="FSpec" name="mFModel" width="100" headerAlign="center">规格型号</div>
                        <div field="FUnitName" name="mFUnitName" width="40" headerAlign="center">单位</div>
                        <div field="FValue" name="FValue" width="70" numberFormat="#,0.00" headerAlign="center" align="right">销售数量
                            <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                        </div>
                        <div field="FCode" name="mFCode" width="120" headerAlign="center">商品条码</div>
                    </div>
                </div>
            </div>
            <div id="Storage" name="Storage" title="促销分店" style="height:100%;overflow:hidden;margin-left:20px; margin-top:5px;">
                <table width = "100%;">
                    <tr>
                        <td width = "200px;">
                            <a class="mini-button" name="AddStock" iconCls="icon-addnew" onclick="getStockItem('')" plain="true">新增</a>
                            <a class="mini-button" name="DelStock" iconCls="icon-remove" onclick="DelStock" plain="true">删行</a>
                        </td>
@*                        <td>
                            <div id="stockall" name="stockall" checked="true"  class="mini-checkbox" readOnly="false" text="所有分店" onvaluechanged="onstocked()"></div>
                        </td>*@
                    </tr>
                </table>  
                <div id="datagrid3" class="mini-datagrid" style="width:100%;height:88%;" 
                    showPager = "false" showPageSize="false" showPageIndex="false" skipReadOnlyCell="true"
                    allowCellSelect="true" allowSortColumn = "false" allowCellEdit="true" navEditMode="true"
                    editNextOnEnterKey="true"  editNextRowCell="false"   multiSelect="true"
                    fitColumns="false">
                    <div property="columns">
                        <div type="checkcolumn" ></div>          
                        <div type="indexcolumn" width="40" headerAlign="center" >序号</div>
                        <div field="FNumber" name="stbm" width="150" headerAlign="center"> 分店编码</div>  
                        <div field="FName" name="stmc" width="300" headerAlign="center">分店名称</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <input class="mini-hidden" id="step" name="step" value="0"/>
    <input class="mini-hidden" id="FItemID" name="FItemID" value="@ViewBag.FItemID"/>
</form>

<script>
    var CustType = [{ "id": "0", "text": "全部" },
    { "id": "1", "text": "非会员" },
    { "id": "2", "text": "会员" }];
    var Gtype = [{ "id": "3", "text": "商品" },
    { "id": "4", "text": "金额" }];

    var datatype = getQueryString("datatype");
    var _Data = new Array(4);
    var _type = 0;

    function FormShown() { }
    $(function () {
        mini.parse();
        var nowDate = new Date();
        mini.getbyName("FStartDate").setValue(nowDate);
        nowDate.setDate(nowDate.getDate() + 1);
        mini.getbyName("FEndDate").setValue(nowDate);
        $("#tabs1").show();
        modify();
        mini.getbyName("week0").setChecked(true);
        mini.getbyName("week1").setChecked(true);
        mini.getbyName("week2").setChecked(true);
        mini.getbyName("week3").setChecked(true);
        mini.getbyName("week4").setChecked(true);
        mini.getbyName("week5").setChecked(true);
        mini.getbyName("week6").setChecked(true);
        BillState(0);
        //mini.getbyName("AddStock").setEnabled(false);
        //mini.getbyName("DelStock").setEnabled(false);
    });
    function NewBill() {
        mini.getbyName("FItemID").setValue("0");
        mini.getbyName("step").setValue("0");
        $("#tabs1").show();
        mini.getbyName("FNumber").setValue("");
        mini.getbyName("FName").setValue("礼券促销");
        mini.get("datagrid1").setData(null);
        mini.get("datagrid3").setData(null);
        SetItemsdisable(true);
        BillState(4);
        mini.getbyName("FStartTime").setValue('00:00');
        mini.getbyName("FEndTime").setValue('23:59');
        var nowDate = new Date();
        mini.getbyName("FStartDate").setValue(nowDate);
        nowDate.setDate(nowDate.getDate() + 1);
        mini.getbyName("FEndDate").setValue(nowDate);
        mini.getbyName("FCouponNumber").setValue(0);
        mini.getbyName("FCouponNumber").setText('');
        mini.getbyName("FCouponName").setValue('');
        createGrid();
    }

    function SetFormControl(form, state) {
        var controls = form.getFields();
        for (var i = 0; i < controls.length; i++) {
            if (controls[i] != undefined) {
                if (state == false) {
                    controls[i].disable();
                }
                else {
                    controls[i].enable();
                }
            }
        }
    }
    function SetItemsdisable(state) {
        var form = new mini.Form("form1");
        SetFormControl(form, state);
        mini.get("datagrid1").setAllowCellEdit(state);
        mini.get("datagrid3").setAllowCellEdit(state);
        mini.getbyName("AddStock").setEnabled(state);
        mini.getbyName("DelStock").setEnabled(state);
        mini.getbyName("AddGood").setEnabled(state);
        mini.getbyName("DelGood").setEnabled(state);
    }
    function check() {
        mini.confirm("确定要审核该方案吗？", "提示",
            function (action) {
                if (action == "ok") {
                    checkBill();
                }
            });
    }
    function checkBill() {
            var jsonstr = '[{"FItemID":"' + mini.getbyName("FItemID").getValue() + '","mSystemAppID":"' + parent.window.getSystemAppID() + '"}]';
            $.post("/coupon/PromotionCheck", { json: jsonstr }, function (data) {
                var ret = mini.decode(data);
                if (ret.status == 1) {
                    mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                    BillState(2);
                }
                else {
                    mini.alert("审核失败:" + ret.error);
                }
            });
    }
    //function onstocked() {
    //    if (mini.getbyName("stockall").getChecked()) {
    //        mini.get("datagrid3").setData(null);
    //        mini.getbyName("AddStock").setEnabled(false);
    //        mini.getbyName("DelStock").setEnabled(false);
    //    }
    //    else {
    //        mini.getbyName("AddStock").setEnabled(true);
    //        mini.getbyName("DelStock").setEnabled(true);
    //    }
    //}

    function save() {
        if (!validata()) {
            mini.alert(Error);
            return;
        }
        var Scheme = GetSchemeJson();
        var ProData = GetProDataJson();
        var objJson = mini.decode(ProData);
        var jsonstr = '{"Scheme":\'' + Scheme + '\',"ProData":\'' + ProData + '\'}';

        $.post("savePromotion", { json: jsonstr, mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                message = mini.decode(ret.msg);
                mini.getbyName("FItemID").setValue(message[0].FItemID);
                mini.getbyName("FNumber").setValue(message[0].Number);
                SetItemsdisable(false);
                BillState(1);
                mini.showTips({ content: message[0].msg, state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                mini.alert("异常:" + ret.error);
            }
        });

    }
    function validata() {
        if (mini.getbyName("FName").getValue() == "") {
            Error = "方案名称未输入！";
            return false;
        }
        if (mini.getbyName("FStartDate").getValue() == "") {
            Error = "开始日期未输入！";
            return false;
        }
        if (mini.getbyName("FEndDate").getValue() == "") {
            Error = "结束日期未输入！";
            return false;
        }
        if ((!mini.getbyName("ckonline").getChecked()) && (!mini.getbyName("ckline").getChecked())) {
            Error = "线上、线下至少要选一个";
            return false;
        }
        if ((mini.get("combo1").getValue()+'') == "") {
            Error = "促销人群未选择";
            return false;
        }
        if (mini.get("datagrid1").getData().length == 0) {
            Error = "未设置商品";
            return false;
        }
        if (mini.getbyName("FCouponNumber").getValue() == "") {
            Error = "未选择礼券";
            return false;
        }
        if (mini.getbyName("FSendQty").getValue() == "") {
            Error = "礼券赠送数量未输入";
            return false;
        }
        var grid3 = mini.get("datagrid3");
        //if (grid3.getData().length == 0) {
        //    Error = "门店未选择";
        //    return false;
        //}

        var t1 = mini.getbyName("FStartDate");
        var t2 = mini.getbyName("FEndDate");
        var startTime = mini.get("FStartTime").getFormattedValue();
        var endTime = mini.get("FEndTime").getFormattedValue();
        var startDate = t1.getFormValue() + ' ' + startTime;
        var endDate = t2.getFormValue() + ' ' + endTime;
        var SDate = new Date(startDate);
        var EDate = new Date(endDate);
        var NDate = new Date();
        if (SDate > EDate) {
            Error = "开始日期不能大于结束日期";
            return false;
        }
        if (NDate > EDate) {
            Error = "结束日期不能小于今日";
            return false;
        }
        return true;
    }
    function GetSchemeJson() {
        var t1 = mini.getbyName("FStartDate");
        var t2 = mini.getbyName("FEndDate");
        var startTime = mini.get("FStartTime").getFormattedValue();
        var endTime = mini.get("FEndTime").getFormattedValue();
        var startDate = t1.getFormValue();
        var endDate = t2.getFormValue();
        var online = 0;
        var line = 0;

        var week = "";
        if (mini.getbyName("week1").getChecked())
            week = "1";
        if (mini.getbyName("week2").getChecked())
            week = week == '' ? "2" : (week + ",2");
        if (mini.getbyName("week3").getChecked())
            week = week == '' ? "3" : (week + ",3");
        if (mini.getbyName("week4").getChecked())
            week = week == '' ? "4" : (week + ",4");
        if (mini.getbyName("week5").getChecked())
            week = week == '' ? "5" : (week + ",5");
        if (mini.getbyName("week6").getChecked())
            week = week == '' ? "6" : (week + ",6");
        if (mini.getbyName("week0").getChecked())
            week = week == '' ? "0" : (week + ",0");
        if (mini.getbyName("ckonline").getChecked())
            online = 1;
        if (mini.getbyName("ckline").getChecked())
            line = 1;
        var json = '{"FItemID":"' + mini.getbyName("FItemID").getValue() + '","FNumber":"' + mini.getbyName("FNumber").getValue() + '","FName":"'
            + mini.getbyName("FName").getValue() + '","FStartDate":"' + startDate + '","FTypeName":"礼券促销","FType":"8","setDiscount":1' 
            + ',"FEndDate":"' + endDate + '","FStartTime":"' + startTime + '","FEndTime":"' + endTime + '","FWeekDays":"' + week
            + '","FStockIDs":"' + GetStorageJson() + '","FRangeID":"' + mini.get("combo2").getValue()
            + '","FMemberClass":"' + mini.get("combo1").getValue() + '","FSendGoodQty":"0"' + ',"FObjectID":"0"'
            + ',"FCouponID":"' + mini.getbyName("FCouponNumber").getValue() + '","FSendCouponQty":"'+mini.getbyName("FSendQty").getValue()+'","FOnLine":"' + online
            + '","FLine":"' + line + '","FDataType":"' + datatype + '"}';
        return json;
    }
    function GetStorageJson() {
        var json = '';
        $.each(mini.get("datagrid3").getData(), function (x, e) {
            if (e.FItemID != undefined) {
                if (json == '')
                    json = json + e.FItemID;
                else
                    json = json + ',' + e.FItemID;
            }
        });
        return json;
    }
    function GetProDataJson() {
        var json = '[';
        $.each(mini.get("datagrid1").getData(), function (x, e) {
                if (json != '[')
                    json += ',';
                if (mini.get("combo2").getValue() == 4) {
                    json = json + '{"FItemID":' + e.FItemID + ',"FRangeID":"4","FRangeExt":0,"FJudgeID":0,"FMin":0,"FMax":0,"FValue":"' + e.FValue + '","FMallValue":"' + e.FValue + '"}';
                }
                else if (e.FItemID != undefined) {
                    json = json + '{"FItemID":' + e.FItemID + ',"FRangeID":"' + mini.get("combo2").getValue() + '","FRangeExt":'
                            + e.FGoodID + ',"FJudgeID":0,"FMin":"0","FMax":"0","FValue":"' + e.FValue + '","FMallValue":"0"}';
            }
        });
        json = json + ']';
        if (json == '[]') {
            json = '[{"FItemID":0,"FRangeID":"' + mini.get("combo2").getValue() + '","FRangeExt":0,"FJudgeID":0,"FMin":"0",'
                + '"FMax":"0","FValue":"0","FMallValue":' + mini.getbyName("setDiscount").getValue() + '}]';
        }
        return json;
    }

    function BillState(flag) {
        var grid = mini.get("datagrid1");
        var gridstock = mini.get("datagrid3");
        switch (flag) {
            case 0:   //新增
                mini.get("check").setEnabled(false);
                mini.get("save").setEnabled(true);
                mini.get("modify").setEnabled(false);
                grid.setAllowCellEdit(true);
                gridstock.setAllowCellEdit(true);
                break;
            case 1:   //保存
                mini.get("check").setEnabled(true);
                mini.get("save").setEnabled(false);
                mini.get("modify").setEnabled(true);
                grid.setAllowCellEdit(false);
                gridstock.setAllowCellEdit(false);
                break;
            case 2:   //审核
                mini.get("check").setEnabled(false);
                mini.get("save").setEnabled(false);
                mini.get("modify").setEnabled(false);
                grid.setAllowCellEdit(false);
                gridstock.setAllowCellEdit(false);
                break;
            case 3:   //关帐
                mini.get("check").setEnabled(false);
                mini.get("save").setEnabled(false);
                mini.get("modify").setEnabled(false);
                grid.setAllowCellEdit(false);
                gridstock.setAllowCellEdit(false);
                break;
            case 4:   //修改后            
                mini.get("check").setEnabled(false);
                mini.get("save").setEnabled(true);
                mini.get("modify").setEnabled(false);
                grid.setAllowCellEdit(true);
                gridstock.setAllowCellEdit(true);
                break;
        }
    }
    function Edit() {
        BillState(4);
        SetItemsdisable(true);
        onstocked();
        mini.getbyName("FNumber").setEnabled(false);
    }

    function setCoupon(FCouponID) {
        var json = "[{'mSystemAppID':'" + parent.window.getSystemAppID() + "','FItemID':" + FCouponID + "}]";
        $.post("../coupon/GetCoupon", {json: json}, function (data) {
            var Data = mini.decode(data);
            mini.getbyName("FCouponNumber").setValue(Data.data[0].FItemID);
            mini.getbyName("FCouponNumber").setText(Data.data[0].FNumber);
            mini.getbyName("FCouponName").setValue(Data.data[0].FName);
        });
    }

    function modify() {
        var AppID = parent.window.getSystemAppID();
        if (parseInt(mini.getbyName("FItemID").getValue()) > 0) {
            $.post("getPromotionInfo", { json: '{"FItemID":"' + mini.getbyName("FItemID").getValue() + '","mSystemAppID":"' + AppID + '"}' }, function (data) {
                var form = new mini.Form("form1");
                var mdata = mini.decode(data);
                var Scheme = mini.decode(mdata.Scheme[0]);
                var Good = mini.decode(mdata.Good[0]);
                form.setData(Scheme);
                var weekArray = Scheme.FWeekDays.split(',');
                $.each(weekArray, function (index, e) {
                    if (e == '')
                        return true;
                    switch (e) {
                        case '0': mini.getbyName("week0").setChecked(true); break;
                        case '1': mini.getbyName("week1").setChecked(true); break;
                        case '2': mini.getbyName("week2").setChecked(true); break;
                        case '3': mini.getbyName("week3").setChecked(true); break;
                        case '4': mini.getbyName("week4").setChecked(true); break;
                        case '5': mini.getbyName("week5").setChecked(true); break;
                        case '6': mini.getbyName("week6").setChecked(true); break;
                    }
                });
                if (Scheme.FOnLine)
                    mini.getbyName("ckonline").setChecked(true);
                if (Scheme.FLine)
                    mini.getbyName("ckline").setChecked(true);
                mini.get("combo1").setValue(Scheme.FBuyerID);
                mini.get("combo2").setValue(Scheme.FRangeID);
                mini.get("FStartTime").setValue(Scheme.FStartTime);
                mini.get("FEndTime").setValue(Scheme.FEndTime);
                mini.getbyName("FSendQty").setValue(Scheme.FSendCouponQty);
                setCoupon(Scheme.FCouponID);
                mini.getbyName("step").setValue(Scheme.FSendCouponQty);
                //if (mdata.Stock.length == 0)
                //    mini.getbyName("stockall").setChecked(true);
                //else
                    setGrid(mdata.Stock, "datagrid3");
                createGrid();
                setGrid(mdata.Good, "datagrid1");
                setCoupon(Scheme.FCouponID);
                //switch (Scheme.FState) {
                //    case -1: mini.getbyName("FState").setValue("已停用"); break;
                //    case 0: mini.getbyName("FState").setValue("未审核"); break;
                //    case 1: mini.getbyName("FState").setValue("已审核"); break;
                //}
                if (Scheme.FChecked || Scheme.FState == -1) {
                    BillState(2);
                    SetItemsdisable(false);
                }
                else {
                    BillState(1);
                    SetItemsdisable(false);
                }
            });
        }
        else {
            //$.post("NewNumber", { mSystemAppID: parent.window.getSystemAppID() }, function (data) {
            //    mini.getbyName("FNumber").setValue(data);
            //});
        }
    }
    function setGrid(action, gridname) {
        if (action) {
            var grid = mini.get(gridname);
            for (var i = 0, l = action.length; i < l; i++) {
                var rw = action[i];
                var newRow;
                if (gridname == "datagrid1")
                    newRow = {
                        FItemID: rw.FItemID, FGoodID: rw.GoodID, FNumber: rw.GoodNumber, FName: rw.GoodName, FSpec: rw.FSpec, FUnitName: rw.FUnitName, FValue: rw.FValue,
                        FSPrice: rw.FPosPrice, FPrice: rw.FValue, FSMallPrice: rw.FMallPrice, FMallPrice: rw.FMallValue, FCode: rw.FCode
                        /*, FRemark: rw.FRemark*/
                    };
                else
                    newRow = { FItemID: rw.FItemID, FNumber: rw.FNumber, FName: rw.FName };
                if (!ExistsInGrid(rw.FItemID, gridname)) {
                    grid.addRow(newRow, grid.getData().length);
                }
            }
        }
    }

    function ExistsInGrid(FItemID, gridName) {
        var grid = mini.get(gridName);
        var ret = false;
        $.each(grid.getData(), function (x, e) {
            if (e.FItemID == FItemID) {
                ret = true;
                return false;
            }
        });
        return ret;
    }
    function ExistsGoodInGrid(ID, gridName) {
        var grid = mini.get(gridName);
        var ret = false;
        $.each(grid.getData(), function (x, e) {
            if (e.FGoodID == ID) {
                ret = true;
                return false;
            }
        });
        return ret;
    }

    function ShowGoodsWin(txtValue) {
        mini.open({
            url: "../Bill/GetBillGoods",
            title: "选择商品", width: 900, height: 500,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { key: txtValue, mBillType: "0" };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action == "close") return;
                if (action) {
                    var grid = mini.get("datagrid1");
                    for (var i = 0, l = action.length; i < l; i++) {
                        var rw = action[i];
                        if (ExistsGoodInGrid(rw.FItemID, "datagrid1"))
                            continue;
                        var newRow = {
                            FItemID: 0, FGoodID: rw.FItemID, FNumber: rw.FNumber, FName: rw.FName, FSpec: rw.FSpec,
                            FUnitName: rw.FUnitName, FValue: 1, FCode: rw.FCode
                        };
                        grid.addRow(newRow, grid.getData().length);
                    }
                }
            }
        });
    }

    function remove() {
        var grid = mini.get("datagrid1");
        var row = grid.getSelected();
        if (row) {
            grid.removeRow(row);
        }
    }
    function addnew() {
        if (mini.get("combo2").getValue() == 3)
            ShowGoodsWin("");
        else {
            var grid = mini.get("datagrid1");
            if(grid.getData().length==0)
                grid.addRow({ FItemID: 0, FValue: 20 });
        }
    }

    function getStockItem(mSearchKey) {
        mini.open({
            url: "../GetItem/Multindex",
            title: "门店选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "StockRelevant", sSearchKey: mSearchKey };
                iframe.contentWindow.SetData(data);//打开GetItemID.js的SetData
            },
            ondestroy: function (action) {
                if (action) {
                    var grid = mini.get("datagrid3");
                    for (var i = 0, l = action.length; i < l; i++) {
                        grid.addRow({ FItemID: action[i].FItemID, FNumber: action[i].FNumber, FName: action[i].FName }, mini.get("datagrid3").getData().length);
                    }
                }
            }
        });
    }
    function DelStock() {
        mini.confirm("确定要删除选择的记录吗？", "提示",
            function (action) {
                if (action == "ok") {
                    var grid = mini.get("datagrid3");
                    var rows = grid.getSelecteds();
                    $.each(rows, function (index, e) {
                        grid.removeRow(e);
                    });
                }
            });
    }

    function onComboxChanged() {
        var grid = mini.get("datagrid1");
        _Data[_type] = grid.getData();
        createGrid(mini.get("combo2").getValue());
        grid.setData(_Data[mini.get("combo2").getValue()]);
        _type = mini.get("combo2").getValue();
    }
    function createGrid() {
        var grid = mini.get("datagrid1");
        if (mini.get("combo2").getValue() == 4) {
            grid.set({
                columns: [
                        { type: "indexcolumn", header: "序号", width: 35 },
                        { field: "FValue", width: 70, headerAlign: "center", Align: "right", header: "销售金额", editor: { type: "spinner", minValue: 0, maxValue: 999999999 } }
                ]
            });
        }
        else if (mini.get("combo2").getValue() == 3)
            grid.set({
                columns: [
                    { type: "indexcolumn", header: "序号", width: 35 },
                    { field: "FNumber", width: 120, headerAlign: "center", allowSort: true, header: "商品编码" },
                    { field: "FName", width: 160, headerAlign: "center", allowSort: true, header: "商品名称" },
                    { field: "FSpec", width: 100, headerAlign: "center", allowSort: true, header: "规格型号" },
                    { field: "FUnitName", width: 40, headerAlign: "center", allowSort: true, header: "单位" },
                    { field: "FValue", width: 70, headerAlign: "center", align: "right", header: "销售数量", editor: { type: "spinner", minValue: 0, maxValue: 999999 } },
                    { field: "FCode", width: 120, headerAlign: "center", header: "商品条码" }
                ]
            });
        grid.clearRows();
    }

</script>